package net.googlecode.garzha.dao;

import net.googlecode.garzha.domains.Comment;
import net.googlecode.garzha.domains.Group;
import net.googlecode.garzha.domains.Person;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 01.06.11
 * Time: 15:38
 * To change this template use File | Settings | File Templates.
 */

@Repository
public class PersonDAOImpl implements PersonDAO{

    @Autowired
	private SessionFactory sessionFactory;

    public Person getPerson(String email){

        return (Person)sessionFactory.getCurrentSession().createQuery("from Person where email=?").setString(0,email).uniqueResult();


    }
        public Person getPersonID(String id){

        return (Person)sessionFactory.getCurrentSession().createQuery("from Person where id=?").setString(0,id).uniqueResult();


    }

   public void updatePerson(Person prs){
      sessionFactory.getCurrentSession().update(prs);

     }
     public void addPerson(Person prs){
      sessionFactory.getCurrentSession().save(prs);

     }
    public void addComment(Comment com){
      sessionFactory.getCurrentSession().save(com);

     }
    public Group getGroup(Integer id){

        return (Group)sessionFactory.getCurrentSession().createQuery("from Group where id=?").setString(0,id.toString()).uniqueResult();


    }

      public List getGroupMates(Person prs){
        return sessionFactory.getCurrentSession().createQuery("select id,fname,sname,imgurl from Person where grpid=? and email != ?")
                .setParameter(0,prs.getGrpid())
                .setString(1,prs.getEmail()).list();
      }
      public List getListComment(String idn){
        return sessionFactory.getCurrentSession().createQuery("select id,comment,guestid from Comment  where personid=? order by id desc")
                .setString(0, idn)
                .list();
      }
     public List getListGroup(){
       return sessionFactory.getCurrentSession()
               .createQuery("select id,name from Group")
               .list();

     }
     public List getTop5(){
       return sessionFactory.getCurrentSession()
               .createQuery("select id,fname,sname,imgurl from Person order by rate desc ")
               .setFirstResult(0).setMaxResults(5).list();
     }
}
